import VueRouter from "vue-router"

import githubSearch from '@/pagers/GithubSearch/App_GithubSearch'
import myTodoList from '@/pagers/MyTodoList/TodoListApp'
import useAxios from '@/pagers/UseAxios/UseAxios'
import useMixin from '@/pagers/UseMixin/UseMixin'
import usePlugin from '@/pagers/UsePlugins/UsePlugin'
import useVuex from '@/pagers/UseVuex/App'
import useRender from '@/pagers/VueRenderMethod/UseRenderMethod'

import nestMessage from '@/pagers/NestPages/Message'
import messageDetils from '@/pagers/NestPages/MessageDetils'
import nest from '@/pagers/NestPages/Nest'
import nestNews from '@/pagers/NestPages/News'

const router=new VueRouter({
    mode:'history', //不写，url中就有#， 这种模式就没有#
    routes:[
        {name:'headSearch' , path:'/githubSearch',component: githubSearch},
        {name:'showlist' , path:'/myTodoList',component: myTodoList},
        {name:'myAxios' ,  path:'/useAxios',component: useAxios},
        {name:'myMixin', path:'/useMixin',component: useMixin},
        {path:'/usePlugin',component: usePlugin},
        {path:'/useVuex',component: useVuex},
        {path:'/useRender',component: useRender},
        {path:'/nest',component: nest, children:[
           {name:'news' , path:'news',component:nestNews,meta:{isAuth:true,title:'新闻页'}},
           {path:'message',component:nestMessage, children:[
                {name:'messageDetils' ,path:'messageDetils',component:messageDetils,meta:{title:'信息页'},
                beforeEnter(to,from,next){
                    console.log('独享守卫-beforeEnter',to,from)
                    if(localStorage.getItem('name')==='xiaoFeng'){
                        next()
                    }else{
                        alert('你无权限访问该组件！！')
                    }
                }}
           ]},

        ]},
    ]
})

//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to,from,next)=>{
    console.log('全局-前置路由守卫',to,from)
    if(to.meta.isAuth){
        if(localStorage.getItem('name')==='xiaoFeng'){
            next()
        }else{
            alert('你无权限访问该组件！！')
        }
    }else{
        next()
    }
    
})

//全局后置路由守卫————初始化的时候被调用、每次路由切换之后被调用
router.afterEach((to,from)=>{
    console.log('全局-后置路由守卫',to,from)
    document.title=to.meta.title || 'Vue2练习'
})

export default router